<template>
  <div class="container-message">
    <el-form ref="formRef" :model="params" inline label-width="80px">
      <el-form-item prop="title" label="标题">
        <el-input v-model="params.title" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item prop="publisher" label="发布者">
        <el-input v-model="params.publisher" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="timer" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" type="datetimerange" range-separator="-"
          start-placeholder="开始时间" end-placeholder="结束时间" @change="timerChange">
        </el-date-picker>
      </el-form-item>
      <el-form-item prop="title" label="是否推荐">
        <el-select v-model="params.recommend" placeholder="请选择">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getDataList()">查询</el-button>
        <el-button type="warning" @click="resetPamars()">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="add">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" v-loading="loading" style="width: 100%">
      <el-table-column label="序号" align="center" width="80">
        <template slot-scope="{$index}">
          <div>
            {{ $index + (params.page - 1) * params.limit + 1 }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title"></el-table-column>
      <el-table-column label="分类" align="center" prop="classify_name"></el-table-column>
      <el-table-column label="浏览量" align="center" prop="browse_sum" width="100"></el-table-column>
      <el-table-column label="发布者" align="center" prop="publisher"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="create_time"></el-table-column>
      <el-table-column label="推荐状态" align="center" prop="recommend">
        <template slot-scope="{row}">
          <div>
            <el-switch v-model="row.recommend" active-color="#13ce66" inactive-color="#B9B9B9" @change="changeRecommendStatus(row.id)">
            </el-switch>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="排序号" align="center" prop="sort" width="80"></el-table-column>
      <el-table-column label="置顶状态" align="center" prop="top">
        <template slot-scope="{ row }">
          <div>
            {{ row.stick ? '已置顶' : '未置顶' }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="260">
        <template slot-scope="{row}">
          <div>
            <el-button type="text" v-if="!row.stick" @click="sysTop(row.id)">设置置顶</el-button>
            <el-button type="text" v-else @click="closeTop(row.id)">取消置顶</el-button>
            <el-button type="text" @click="openDetail('look', row.id)">查看详情</el-button>
            <el-button type="text" @click="openDetail('edit', row.id)">编辑</el-button>
            <el-button type="text" @click="del(row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="params.page" :page-sizes="[10, 20, 50, 100]"
      :page-size="params.limit" :total="totalPage" layout="total, sizes, prev, pager, next, jumper"></el-pagination>

    <el-dialog title="置顶设置" center :close-on-click-modal="false" :visible.sync="topDialogVisible" width="30%" :before-close="handleClose">
      <el-form ref="topFormRef" :model="topForm" :rules="topFormRules" label-width="120px">
        <el-form-item label="置顶结束时间" prop="top_end_time">
          <el-date-picker v-model="topForm.top_end_time" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss" type="datetime" range-separator="-"
            placeholder="请选择时间" :disabled="topForm.stick">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="!topForm.stick" @click="submintHandle">开始置顶</el-button>
        <el-button type="primary" v-else @click="closeTopHandle">取消置顶</el-button>
        <el-button @click="handleClose">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import seviceApi from '@/api/serviceApi.js'
export default {
  data () {
    return {
      params: {
        page: 1,
        limit: 10,
        title: '',
        publisher: '',
        recommend: '',
        start_time: null,
        end_time: null
      },
      timer: null,
      options: [
        {
          i: 0,
          label: '全部',
          value: ''
        },
        {
          i: 1,
          label: '是',
          value: '1'
        },
        {
          i: 2,
          label: '否',
          value: '2'
        }
      ],
      tableData: [],
      loading: false,
      totalPage: 0,
      topDialogVisible: false,
      topForm: {
        top_end_time: ''
      },
      topFormRules: {
        top_end_time: { required: true, message: '置顶结束时间不能为空', trigger: 'blur' }
      },
      id: ''
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.loading = true
      seviceApi.getInformationList(this.params).then(res => {
        if (res && res.data.code == 200) {
          this.tableData = res.data.page.list
          this.totalPage = res.data.page.totalCount
          this.loading = false
        }
      })
    },
    timerChange (e) {
      console.log(e)
      if (e) {
        this.params.start_time = e[0]
        this.params.end_time = e[1]
      } else {
        this.params.start_time = null
        this.params.end_time = null
      }
    },
    // 每页数
    sizeChangeHandle (val) {
      this.params.limit = val
      this.getList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.params.page = val
      this.getList()
    },
    // 查询
    getDataList () {
      this.params.page = 1
      this.getList()
    },
    resetPamars () {
      this.timer = null
      this.params.start_time = null
      this.params.end_time = null
      this.$refs.formRef.resetFields()
      this.getDataList()
    },
    changeRecommendStatus (id) {
      seviceApi.changeInformationStatus(id).then(res => {
        if (res && res.data.code == 200) {
          this.$message.success('修改成功')
        }
      })
    },
    add () {
      this.$router.push({
        path: '/information/add-detail',
        query: {
          type: 'add'
        }
      })
    },
    sysTop (id) {
      this.topDialogVisible = true
      this.id = id
    },
    closeTop (id) {
      this.id = id
      seviceApi.getInformationInfo(id).then(res => {
        if (res && res.data.code == 200) {
          this.topDialogVisible = true
          this.$nextTick(() => {
            this.topForm.top_end_time = res.data.data.top_end_time
            this.topForm.stick = res.data.data.stick
          })
        }
      })
      // this.$confirm('是否取消该咨询置顶, 是否继续?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      //   this.changeTop()
      // }).catch(() => {
      //   this.$message({
      //     type: 'info',
      //     message: '已取消'
      //   });
      // });
    },
    // 修改置顶状态
    changeTop () {
      seviceApi.changeTopStatus({
        id: this.id,
        ...this.topForm
      }).then(res => {
        if (res && res.data.code == 200) {
          this.$message.success('操作成功')
          this.handleClose()
          this.getList()
        }
      })
    },
    handleClose () {
      this.topDialogVisible = false
      this.topForm.top_end_time = ''
      this.topForm.stick = false
      this.$refs.topFormRef.resetFields()
    },
    submintHandle () {
      this.$refs.topFormRef.validate((valid) => {
        if (valid) {
          this.changeTop()
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    closeTopHandle () {
      this.changeTop()
    },
    openDetail (type, id) {
      this.$router.push({
        path: '/information/add-detail',
        query: {
          type,
          id
        }
      })
    },
    del (id) {
      this.$confirm('是否删除该条咨询, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        seviceApi.deleteInformation(id).then(res => {
          if (res && res.data.code == 200) {
            this.$message.success('操作成功')
            this.getList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    }
  }
}
</script>

<style lang="scss" scoped></style>